library(readr)
library(car)
library(ggplot2)
library(ggpubr)
library(rstatix)
library(dplyr)
data<-read_rds("sk_pobe_cleandata.rds")

#########################Figs and Tables in main texts#########################
#Fig 1
fig1.test <- data %>% 
  t_test(self_contribution ~ treatment) %>%
  add_significance("p")
fig1.test

fig1_test_p<-fig1.test$p
fig1_test_p<-fig1.test[8]
fig1_test_p
fig1_test_p$p<-format(fig1_test_p$p,2,digits=2) 
fig1_test_p
table(data$self_contribution)

png("Fig 1.png", width=600,height=600,units="px",res=120,pointsize=8)

ggplot(data=data,aes(x=treatment, y=self_contribution, fill=treatment),outlier.size = -1)+
  stat_summary(geom = "bar", fun = mean, position = "identity", color="black")+
  stat_summary(geom = "linerange", fun.data = mean_se, 
               position="identity")+
  stat_compare_means(
    label.y=95,
    method="t.test",
    symnum.args=list(cutpoints=c(0.1,1),symbols = sprintf("p = %s", fig1_test_p[1])),
    comparisons=list(c("Control","Treatment")))+
  theme(legend.position = "none")+
  scale_fill_grey(start = .6, end = 1)+
  coord_cartesian(ylim=c(0,125)) +
  labs(x="Discrimination", 
       y="Public Good Contribution
       (0-150)") + theme(text=element_text(size=10, family="sans", face="bold"))

dev.off()

t_test(self_contribution~treatment,data=subset(data,failed_too_many==0),detailed=T)
cohens_d(self_contribution~treatment,data=subset(data,failed_too_many==0),nboot=10000,ci=T)

t_test(self_contribution~treatment,data=subset(data,fullcomp==1),detailed=T)
cohens_d(self_contribution~treatment,data=subset(data,fullcomp==1),nboot=10000,ci=T)

#Fig 2
library(sjPlot)
library(estimatr)
fig2mod1_robust<-lm_robust(self_contribution~dyadicntl*treatment,se_type="HC2",data=data)
fig2mod2_robust<-lm_robust(expamt_partner~dyadicntl*treatment,se_type="HC2",data=data)

fig2mod1plot<-plot_model(fig2mod1_robust, 
                         type = "int", 
                         ci.lvl = 0.95,
                         colors=c("grey50","black"),
                         alpha = 0.15,
                         title="A. DV: PG Contribution (0-150)",
                         auto.label = F,
                         axis.title = c("Dyadic Ethnicity",""),
                         legend.title = "Discrimination",
                         dodge = 0.5) + 
  geom_bar(position=position_dodge(), stat = "identity", width=0.5, alpha=0.3)+ 
  coord_cartesian(ylim=c(40,90),xlim=c(0.5,2.5))+
  theme(text=element_text(size=10,  face="bold", family="sans"))  
fig2mod1plot

fig2mod2plot<-plot_model(fig2mod2_robust, 
                         type = "int", 
                         ci.lvl = 0.95,
                         colors=c("grey50","black"),
                         alpha = 0.15,
                         title="B. DV: Exp. Partner Contribution (0-150)",
                         auto.label = F,
                         axis.title = c("Dyadic Ethnicity",""),
                         legend.title = "Discrimination",
                         dodge = 0.5) + 
  geom_bar(position=position_dodge(), stat = "identity", width=0.5, alpha=0.3)+ 
  coord_cartesian(ylim=c(30,80),xlim=c(0.5,2.5))+
  theme(text=element_text(size=10,  face="bold", family="sans"))  
fig2mod2plot

png("Fig 2.png", width=600,height=600,units="px",res=120,pointsize=8)

gridExtra::grid.arrange(fig2mod1plot,fig2mod2plot,nrow=2)

dev.off()

#Fig 3
data_out<-subset(data,dyadicntl=="Out-Ethnic")
data_co<-subset(data,dyadicntl=="Co-Ethnic")

fig3mod1_robust<-lm_robust(self_contribution ~ (Asian_ID_Centered+Ethnic_ID_Centered)*treatment+treatment, se_type="HC2", data=data_out)
fig3mod2_robust<-lm_robust(self_contribution ~ (Asian_ID_Centered+Ethnic_ID_Centered)*treatment+treatment, se_type="HC2", data=data_co)

fig3mod1plot<-plot_model(fig3mod1_robust,
                         grid=F,
                         type="int",
                         mdrt.values = "all",
                         digits=3,
                         value.size=3.5,
                         alpha=0.15,
                         title="",
                         legend.title = "Discrimination",
                         axis.title = "PG Contribution",
                         colors=c("grey60","black"),
                         auto.label = F)

fig3mod1plot[[1]]$labels$x<-"Asian ID (Centered)"
fig3mod1plot[[1]]$labels$title<-"A"
fig3mod1plot[[2]]$labels$x<-"Ethnic ID (Centered)"
fig3mod1plot[[2]]$labels$title<-"C"

fig3mod1aplot<-fig3mod1plot[[1]]
fig3mod1bplot<-fig3mod1plot[[2]]

fig3mod1aplot<-fig3mod1aplot+theme(text=element_text(size=10,  family="sans", face="bold"))
fig3mod1bplot<-fig3mod1bplot+theme(text=element_text(size=10,  family="sans", face="bold"))
fig3mod1aplot
fig3mod1bplot

fig3mod2plot<-plot_model(fig3mod2_robust,
                         grid=F,
                         type="int",
                         mdrt.values = "all",
                         digits=3,
                         value.size=3.5,
                         alpha=0.15,
                         title="",
                         legend.title = "Discrimination",
                         axis.title = "PG Contribution",
                         colors=c("grey60","black"),
                         auto.label = F)

fig3mod2plot[[1]]$labels$x<-"Asian ID (Centered)"
fig3mod2plot[[1]]$labels$title<-"B"
fig3mod2plot[[2]]$labels$x<-"Ethnic ID (Centered)"
fig3mod2plot[[2]]$labels$title<-"D"

fig3mod2aplot<-fig3mod2plot[[1]]
fig3mod2bplot<-fig3mod2plot[[2]]

fig3mod2aplot<-fig3mod2aplot+theme(text=element_text(size=10,  family="sans", face="bold"))
fig3mod2bplot<-fig3mod2bplot+theme(text=element_text(size=10,  family="sans", face="bold"))
fig3mod2aplot
fig3mod2bplot

png("Fig 3.png", width=900,height=650,units="px",res=120,pointsize=8)

gridExtra::grid.arrange(fig3mod1aplot,fig3mod2aplot,fig3mod1bplot,fig3mod2bplot, ncol=2)

dev.off()

#Fig 4
fig4.test <- data %>% 
  t_test(outgroup_imm ~ treatment) %>%
  add_significance("p")
fig4.test

fig4_test_p<-fig4.test$p
fig4_test_p<-fig4.test[8]
fig4_test_p
fig4_test_p$p<-format(fig4_test_p$p,2,digits=2) 
fig4_test_p
table(data$outgroup_imm)

png("Fig 4.png", width=600,height=600,units="px",res=120,pointsize=8)

ggplot(data=data,aes(x=treatment, y=outgroup_imm, fill=treatment),outlier.size = -1)+
  stat_summary(geom = "bar", fun = mean, position = "identity", color="black")+
  stat_summary(geom = "linerange", fun.data = mean_se, 
               position="identity")+
  stat_compare_means(
    label.y=.65,
    method="t.test",
    symnum.args=list(cutpoints=c(0.1,1),symbols = sprintf("p = %s", fig4_test_p[1])),
    comparisons=list(c("Control","Treatment")))+
  theme(legend.position = "none")+
  scale_fill_grey(start = .6, end = 1)+
  coord_cartesian(ylim=c(0,.75)) +
  labs(x="Discrimination", 
       y="Support for Immigration from Disparate Asian Countries
       (0-1)") + theme(text=element_text(size=10, family="sans", face="bold"))

dev.off()

t_test(outgroup_imm~treatment,data=data,detailed=T)
cohens_d(outgroup_imm~treatment,data=data,nboot=10000,ci=T)

t_test(outgroup_imm1~treatment,data=data,detailed=T)
cohens_d(outgroup_imm1~treatment,data=data,nboot=10000,ci=T)

t_test(outgroup_imm2~treatment,data=data,detailed=T)
cohens_d(outgroup_imm2~treatment,data=data,nboot=10000,ci=T)

t_test(selfntl_imm~treatment,data=data,detailed=T)
cohens_d(selfntl_imm~treatment,data=data,nboot=10000,ci=T)

t_test(france~treatment,data=data,detailed=T)
cohens_d(france~treatment,data=data,nboot=10000,ci=T)

t_test(germany~treatment,data=data,detailed=T)
cohens_d(germany~treatment,data=data,nboot=10000,ci=T)

t_test(mexico~treatment,data=data,detailed=T)
cohens_d(mexico~treatment,data=data,nboot=10000,ci=T)

t_test(ukraine~treatment,data=data,detailed=T)
cohens_d(ukraine~treatment,data=data,nboot=10000,ci=T)

#Table 1
tab1mod<-lm(outgroup_imm ~ (Asian_ID_Centered)*treatment+treatment, data=data)

stargazer::stargazer(tab1mod,
                     type="html",
                     model.names=F,
                     se=starprep(tab1mod),
                     p=starprep(tab1mod,stat="p.value"),
                     font.size = "scriptsize", 
                     out="Table1.html",
                     dep.var.caption = "Dependent Variable",
                     covariate.labels=c(
                       "Asian ID",
                       "Treatment",
                       "Asian ID x Treatment",
                       "Constant"
                     ),
                     omit.stat = c("adj.rsq","f","ser"),
                     notes.align="l",
                     notes.append=F,
                     notes.label="<font size=0.9><i>Notes: Entries are OLS coefficients with robust standard errors in parentheses.</i><br><sup>*</sup>p<0.1<sup>**</sup>p<0.05<sup>***</sup>p<0.01</font>",
                     notes="",
                     dep.var.labels = "Support for Immigration from Disparate Asian Countries"
)

#########################Figs and Tables in supplemental info#########################
#Table S1.1
alldata_df<-data[c("female",
                   "bachelors",
                   "age",
                   "chinese",
                   "filipino",
                   "indian",
                   "japanese",
                   "korean",
                   "vietnamese",
                   "nativity",
                   "inc",
                   "asianids",
                   "ethnicids",
                   "dem",
                   "lib",
                   "polint",
                   "dyadicntl_num",
                   "self_contribution",
                   "expamt_partner",
                   "bot_contribution",
                   "bot_matched_num")]

stargazer::stargazer(alldata_df,
                     type = "html", 
                     out="TableS1_1.html",
                     covariate.labels = c("Gender (Female = 1)",
                                          "College Degree",
                                          "Age",
                                          "Chinese",
                                          "Filipino/a",
                                          "Indian",
                                          "Japanese",
                                          "Korean",
                                          "Vietnamese",
                                          "Born in the US",
                                          "Income",
                                          "Asian ID",
                                          "Ethnic ID",
                                          "Democrat",
                                          "Liberal",
                                          "Political Interest",
                                          "Dyadic Shared Ethnicity",
                                          "PG Contribution",
                                          "Expected Partner's PG Contribution",
                                          "Bot Player PG Contribution",
                                          "Matched with a Bot"
                     ),
                     model.names=F,
                     notes.align = "l",
                     digits=2,
                     median=T,
                     iqr=T)

#Table S1.2
tabs12data_df<-data[c("treatment",
                      "female",
                      "bachelors",
                      "age",
                      "chinese",
                      "filipino",
                      "indian",
                      "japanese",
                      "korean",
                      "vietnamese",
                      "nativity",
                      "inc",
                      "asianids",
                      "ethnicids",
                      "dem",
                      "lib",
                      "failed_too_many",
                      "dyadicntl_num",
                      "bot_contribution",
                      "bot_matched_num")]

tabs12data_df <- tabs12data_df |>
  select(`Gender (Female = 1)` = female,
         `College Degree` = bachelors,
         `Age` = age,
         `Chinese` = chinese,
         `Filipino` = filipino,
         `Indian` = indian,
         `Japanese` = japanese,
         `Korean` = korean,
         `Vietnamese` = vietnamese,
         `US Born` = nativity,
         `Income` = inc,
         `Asian ID` = asianids,
         `Ethnic ID` = ethnicids,
         `Democrat` = dem,
         `Liberal` = lib,
         `Comprehension Score < 50%` = failed_too_many,
         `Dyadic Shared Ethnicity` = dyadicntl_num,
         `Bot Player PG Contribution` = bot_contribution,
         `Matched with a Bot` = bot_matched_num,
         treatment
  )

library(modelsummary)
datasummary_balance(~treatment, data = tabs12data_df, output="TableS1_2.html",
                    fmt = fmt_decimal(digits=2), stars=F,dinm=F)

#library(cobalt)
#data %>% select(treatment) %>% table
#bal.tab(treatment_num~female+bachelors+age+chinese+filipino+indian+japanese+korean+vietnamese+nativity+inc+asianids+ethnicids+dem+lib+failed_too_many+dyadicntl_num+bot_contribution+bot_matched_num, 
#        data=data, 
#        thresholds=c(m=.1, v=2))
#bal.tab(treatment_num~female+bachelors+age+chinese+filipino+indian+japanese+korean+vietnamese+nativity+inc+asianids+ethnicids+dem+lib+failed_too_many+dyadicntl_num+bot_contribution+bot_matched_num, 
#        data=data, 
#        thresholds=c(m=.1, v=2),imbalanced.only = T)

#Fig S1.1
figs11plot<-ggplot(data,aes(x=asianids,color="grey40")) +
  geom_bar(aes(y = after_stat(count / sum(count))),color="black") +  
  labs(title="") +
  coord_cartesian(xlim=c(-0.05,1.05))+
  scale_x_continuous(name="Asian Identity Strength",n.breaks=7) + 
  scale_y_continuous(name = "Percentage",labels = scales::percent_format(accuracy = 1L))+
  theme(legend.position = "none")+
  geom_vline(data,mapping=aes(xintercept=mean(asianids)),linetype="longdash",color="black") +
  theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S1_1.png", width=850,height=750,units="px",res=120,pointsize=8)

figs11plot

dev.off()

#Fig S1.2
figs12plot<-ggplot(data,aes(x=ethnicids,color="grey40")) +
  geom_bar(aes(y = after_stat(count / sum(count))),color="black") +  
  labs(title="") +
  coord_cartesian(xlim=c(-0.05,1.05))+
  scale_x_continuous(name="Ethnic Identity Strength",n.breaks=7) + 
  scale_y_continuous(name = "Percentage",labels = scales::percent_format(accuracy = 1L))+
  theme(legend.position = "none")+
  geom_vline(data,mapping=aes(xintercept=mean(ethnicids)),linetype="longdash",color="black") +
  theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S1_2.png", width=850,height=750,units="px",res=120,pointsize=8)

figs12plot

dev.off()

#Fig S1.3
figs13plot<-ggplot(data,aes(x=cq_score,color="grey40")) +
  geom_bar(aes(y = after_stat(count / sum(count))),color="black") +  
  labs(title="") +
  coord_cartesian(xlim=c(-0.05,1.05))+
  scale_x_continuous(name="Comprehension Quiz Score",n.breaks=5) + 
  scale_y_continuous(name = "Percentage",labels = scales::percent_format(accuracy = 1L))+
  theme(legend.position = "none")+
  geom_vline(data,mapping=aes(xintercept=mean(cq_score)),linetype="longdash",color="black") +
  theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S1_3.png", width=850,height=750,units="px",res=120,pointsize=8)

figs13plot

dev.off()

#Fig S1.4
data_ctrl<-subset(data,treatment=="Control")

figs14sum <- plyr::ddply(data_ctrl, c("dyadicntl"), summarise, grp.mean=mean(self_contribution))
figs14plot<-ggplot(data_ctrl, aes(x=self_contribution, fill=dyadicntl)) +
  geom_histogram(alpha=0.5, position='identity',
                 aes(y=after_stat(c(
                   count[group==1]/sum(count[group==1]),
                   count[group==2]/sum(count[group==2])
                 )*100)),
                 binwidth=.25) +
  scale_fill_manual(values=c("grey40", "black")) +
  labs(title="",fill="Dyadic Ethnicity") +
  scale_x_continuous(name="PG Contribution",
                     breaks=seq(0,150,by=5)) + 
  scale_y_continuous(name = "Percentage",
                     breaks=seq(0,20,by=1))+
  geom_vline(figs14sum,mapping=aes(xintercept=grp.mean),linetype="longdash",color=c("grey40", "black")) +
  theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S1_4.png", width=975,height=900,units="px",res=120,pointsize=8)

figs14plot

dev.off()

#Fig S1.5
data_tmt<-subset(data,treatment=="Treatment")

figs15sum <- plyr::ddply(data_tmt, c("dyadicntl"), summarise, grp.mean=mean(self_contribution))
figs15plot<-ggplot(data_tmt, aes(x=self_contribution, fill=dyadicntl)) +
  geom_histogram(alpha=0.5, position='identity',
                 aes(y=after_stat(c(
                   count[group==1]/sum(count[group==1]),
                   count[group==2]/sum(count[group==2])
                 )*100)),
                 binwidth=.25) +
  scale_fill_manual(values=c("grey40", "black")) +
  labs(title="",fill="Dyadic Ethnicity") +
  scale_x_continuous(name="PG Contribution",
                     breaks=seq(0,150,by=5)) + 
  scale_y_continuous(name = "Percentage",
                     breaks=seq(0,28,by=1))+
  geom_vline(figs15sum,mapping=aes(xintercept=grp.mean),linetype="longdash",color=c("grey40", "black")) +
  theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S1_5.png", width=975,height=900,units="px",res=120,pointsize=8)

figs15plot

dev.off()

#Fig S1.6
figs16sum <- plyr::ddply(data, c("treatment"), summarise, grp.mean=mean(outgroup_imm))

figs16plot<-ggplot(data, aes(x=outgroup_imm, fill=treatment)) +
  geom_histogram(alpha=0.5, position='identity',
                 aes(y=after_stat(c(
                   count[group==1]/sum(count[group==1]),
                   count[group==2]/sum(count[group==2])
                 )*100)),
                 binwidth=.125) +
  scale_fill_manual(values=c("grey40", "black")) +
  labs(title="",fill="Discrimination") +
  scale_x_continuous(name="Support for Immigration from Disparate Asian Countries",
                     breaks=seq(0,1,by=0.125)) + 
  scale_y_continuous(name = "Percentage",
                     breaks=seq(0,55,by=5))+
  geom_vline(figs16sum,mapping=aes(xintercept=grp.mean),linetype="longdash",color=c("grey40", "black")) +
  theme(text=element_text(size=10,  family="sans", face = "bold"))

png("Fig S1_6.png", width=650,height=750,units="px",res=120,pointsize=8)

figs16plot

dev.off()

#Table S3.1
tabs31mod1<-lm(self_contribution ~ dyadicntl*treatment, data=data)
tabs31mod2<-lm(expamt_partner ~ dyadicntl*treatment, data=data)

stargazer::stargazer(tabs31mod1,tabs31mod2,
                     type="html",
                     model.names=F,
                     se=starprep(tabs31mod1,tabs31mod2),
                     p=starprep(tabs31mod1,tabs31mod2,stat="p.value"),
                     font.size = "scriptsize", 
                     out="TableS3_1.html",
                     dep.var.caption = "Dependent Variables",
                     covariate.labels=c(
                       "Dyadic Ethnicity",
                       "Discrimination",
                       "Dyadic Ethnicity x Discrimination",
                       "Constant"
                     ),
                     omit.stat = c("adj.rsq","f","ser"),
                     notes.align="l",
                     notes.append=F,
                     notes.label="<font size=0.9><i>Notes: Entries are OLS coefficients with robust standard errors in parentheses.</i><br><sup>*</sup>p<0.1<sup>**</sup>p<0.05<sup>***</sup>p<0.01</font>",
                     notes="",
                     dep.var.labels = c("PG Contribution","Exp. Partner Contribution")
)

#Table S3.2
tabs32mod1<-lm(self_contribution ~ TmtNtl, data=data)
tabs32mod2<-lm(expamt_partner ~ TmtNtl, data=data)

stargazer::stargazer(tabs32mod1,tabs32mod2,
                     type="html",
                     model.names=F,
                     se=starprep(tabs32mod1,tabs32mod2),
                     p=starprep(tabs32mod1,tabs32mod2,stat="p.value"),
                     font.size = "scriptsize", 
                     out="TableS3_2.html",
                     dep.var.caption = "Dependent Variables",
                     covariate.labels=c(
                       "Treatment:Out-Ethnic",
                       "Control:Co-Ethnic",
                       "Treatment:Co-Ethnic",
                       "Constant (Control:Out-Ethnic)"
                     ),
                     omit.stat = c("adj.rsq","f","ser"),
                     notes.align="l",
                     notes.append=F,
                     notes.label="<font size=0.9><i>Notes: Entries are OLS coefficients with robust standard errors in parentheses.</i><br><sup>*</sup>p<0.1<sup>**</sup>p<0.05<sup>***</sup>p<0.01</font>",
                     notes="",
                     dep.var.labels = c("PG Contribution","Exp. Partner Contribution")
)

#Table S3.3
tabs33mod1<-lm(self_contribution ~ (Asian_ID_Centered+Ethnic_ID_Centered)*treatment+treatment, data=data_out)
tabs33mod2<-lm(self_contribution ~ (Asian_ID_Centered+Ethnic_ID_Centered)*treatment+treatment, data=data_co)

stargazer::stargazer(tabs33mod1,tabs33mod2,
                     type="html",
                     model.names=F,
                     se=starprep(tabs33mod1,tabs33mod2),
                     p=starprep(tabs33mod1,tabs33mod2,stat="p.value"),
                     font.size = "scriptsize", 
                     out="TableS3_3.html",
                     dep.var.caption = "Dependent Variable",
                     column.labels=c("Dyadic Ethnicity = Out-Ethnic","Dyadic Ethnicity = Co-Ethnic"),
                     covariate.labels=c(
                       "Asian ID",
                       "Ethnic ID",
                       "Discrimination",
                       "Asian ID x Discrimination",
                       "Ethnic ID x Discrimination",
                       "Constant"
                     ),
                     omit.stat = c("adj.rsq","f","ser"),
                     notes.align="l",
                     notes.append=F,
                     notes.label="<font size=0.9><i>Notes: Entries are OLS coefficients with robust standard errors in parentheses. Asian ID and Ethnic ID are centered.</i><br><sup>*</sup>p<0.1<sup>**</sup>p<0.05<sup>***</sup>p<0.01</font>",
                     notes="",
                     dep.var.labels = "PG Contribution"
)

#Table S3.4
tabs34mod<-lm(outgroup_imm ~ TmtNtl1, data=data)

stargazer::stargazer(tabs34mod,
                     type="html",
                     model.names=F,
                     se=starprep(tabs34mod),
                     p=starprep(tabs34mod,stat="p.value"),
                     font.size = "scriptsize", 
                     out="TableS3_4.html",
                     dep.var.caption = "Dependent Variable",
                     covariate.labels=c(
                       "Control:Out-Ethnic",
                       "Treatment:Co-Ethnic",
                       "Treatment:Out-Ethnic",
                       "Constant (Control:Co-Ethnic)"
                     ),
                     omit.stat = c("adj.rsq","f","ser"),
                     notes.align="l",
                     notes.append=F,
                     notes.label="<font size=0.9><i>Notes: Entries are OLS coefficients with robust standard errors in parentheses.</i><br><sup>*</sup>p<0.1<sup>**</sup>p<0.05<sup>***</sup>p<0.01</font>",
                     notes="",
                     dep.var.labels = "Support for Immigration from Disparate Asian Countries"
)

#Fig S3.1
figs31mod_robust<-lm_robust(outgroup_imm ~ Asian_ID_Centered*treatment+treatment, se_type="HC2", data=data)

figs31modplot<-plot_model(figs31mod_robust,
                          grid=F,
                          type="int",
                          mdrt.values = "all",
                          digits=3,
                          value.size=3.5,
                          alpha=0.15,
                          title="Support for Immigration from China and S.Korea (if R is SA) or India and Pakistan (if R is non-SA)",
                          legend.title = "Discrimination",
                          axis.title = c("Asian ID (Centered)",""),
                          colors=c("grey60","black"),
                          auto.label = F)+theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S3_1.png", width=1000,height=800,units="px",res=120,pointsize=8)

figs31modplot

dev.off()

#Fig S3.2
figs32mod1_robust<-lm_robust(self_contribution~TmtNtl,se_type="HC2",data=data)
figs32mod2_robust<-lm_robust(expamt_partner~TmtNtl,se_type="HC2",data=data)

figs32mod1plot<-plot_model(figs32mod1_robust,
                           type="est",
                           ci.lvl = 0.95,
                           show.values=T,
                           show.p=T,
                           digits=3,
                           p.threshold = c(.10,.05,.01),
                           value.size=3.5,
                           colors="black",
                           alpha = 0.15,
                           title="A. PG Contribution (0-150)",
                           axis.labels = c(
                             "Treatment:Co-Ethnic",
                             "Control:Co-Ethnic",
                             "Treatment:Out-Ethnic"
                           ),
                           auto.label = F,
                           axis.title = c("",""),
                           dodge = 0.5)+geom_abline(intercept=0,slope=0,colour="grey70",linetype="longdash")+scale_y_continuous(limits=c(-10,30),n.breaks=6)+theme(text=element_text(size=10,  family="sans", face="bold"))
figs32mod1plot

figs32mod2plot<-plot_model(figs32mod2_robust,
                           type="est",
                           ci.lvl = 0.95,
                           show.values=T,
                           show.p=T,
                           digits=3,
                           p.threshold = c(.10,.05,.01),
                           value.size=3.5,
                           colors="black",
                           alpha = 0.15,
                           title="B. Exp. Partner Contribution (0-150)",
                           axis.labels = c(
                             "Treatment:Co-Ethnic",
                             "Control:Co-Ethnic",
                             "Treatment:Out-Ethnic"
                           ),
                           auto.label = F,
                           axis.title = c("",""),
                           dodge = 0.5)+geom_abline(intercept=0,slope=0,colour="grey70",linetype="longdash")+scale_y_continuous(limits=c(-10,30),n.breaks=6)+theme(text=element_text(size=10,  family="sans", face="bold"))
figs32mod2plot

png("Fig S3_2.png", width=1000,height=800,units="px",res=120,pointsize=8)

gridExtra::grid.arrange(figs32mod1plot,figs32mod2plot,nrow=1)

dev.off()

#Fig S3.3
figs33mod_robust<-lm_robust(outgroup_imm~TmtNtl1,se_type="HC2",data=data)

figs33modplot<-plot_model(figs33mod_robust,
                          type="est",
                          ci.lvl = 0.95,
                          show.values=T,
                          show.p=T,
                          digits=3,
                          p.threshold = c(.10,.05,.01),
                          value.size=3.5,
                          colors="black",
                          alpha = 0.15,
                          title="",
                          axis.labels = c(
                            "Treatment:Out-Ethnic",
                            "Treatment:Co-Ethnic",
                            "Control:Out-Ethnic"
                          ),
                          auto.label = F,
                          axis.title = c("",""),
                          dodge = 0.5)+geom_abline(intercept=0,slope=0,colour="grey70",linetype="longdash")+scale_y_continuous(limits=c(-0.15,0.25),n.breaks=6)+theme(text=element_text(size=10,  family="sans", face="bold"))

png("Fig S3_3.png", width=800,height=800,units="px",res=120,pointsize=8)

figs33modplot

dev.off()

#Fig S3.4
png("Fig S3_4.png", width=600,height=600,units="px",res=120,pointsize=8)

ggplot(data=data, aes(x=bot_matched, y=self_contribution,fill=bot_matched))+
  stat_summary(geom = "bar", fun = mean, position = "identity",color="black")+
  stat_summary(geom = "linerange", fun.data = mean_se, position = "identity")+
  stat_compare_means(method = "t.test", 
                     comparisons = list(c("Matched Partner = Human","Matched Partner = Bot")), 
                     symnum.args = list(cutpoints = c(0, 0.001, 0.01, 0.05, 0.10, 1), 
                                        symbols = c("p < 0.001****", "p < 0.01***", "p < 0.05**", "p < 0.10*", "ns")))+ 
  theme(legend.position = "none")+
  scale_fill_grey(start = .6, end = 1)+
  coord_cartesian(ylim =c(0,115)) +
  labs(x="The Other Player Matched", y="Public Good Contribution
       (0-150)")+ theme(text=element_text(size=10,  family="sans", face="bold"))

dev.off()

t_test(self_contribution~bot_matched,data=data,detailed=T)
cohens_d(self_contribution~bot_matched,data=data,nboot=10000,ci=T)
